home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Interfaces / UniversalInterfaces 2.1B1 / PInterfaces / DigitalSignature.p < prev    next >
Encoding:
Text File  |  1995-04-18  |  16.3 KB  |  413 lines  |  [TEXT/MPS ]

  1. {
  2.      File:        DigitalSignature.p
  3.  
  4.      Contains:    Digital Signature Interfaces.
  5.  
  6.      Version:    Technology:    AOCE toolbox 1.02
  7.                  Package:    Universal Interfaces 2.1ß1 in “MPW Prerelease” on ETO #17
  8.  
  9.      Copyright:    © 1984-1995 by Apple Computer, Inc.
  10.                  All rights reserved.
  11.  
  12.      Bugs?:        If you find a problem with this file, use the Apple Bug Reporter
  13.                  stack.  Include the file and version information (from above)
  14.                  in the problem description and send to:
  15.                      Internet:    apple.bugs@applelink.apple.com
  16.                      AppleLink:    APPLE.BUGS
  17.  
  18. }
  19.  
  20. {$IFC UNDEFINED UsingIncludes}
  21. {$SETC UsingIncludes := 0}
  22. {$ENDC}
  23.  
  24. {$IFC NOT UsingIncludes}
  25.  UNIT DigitalSignature;
  26.  INTERFACE
  27. {$ENDC}
  28.  
  29. {$IFC UNDEFINED __DIGITALSIGNATURE__}
  30. {$SETC __DIGITALSIGNATURE__ := 1}
  31.  
  32. {$I+}
  33. {$SETC DigitalSignatureIncludes := UsingIncludes}
  34. {$SETC UsingIncludes := 1}
  35.  
  36.  
  37. {$IFC UNDEFINED __TYPES__}
  38. {$I Types.p}
  39. {$ENDC}
  40. {    ConditionalMacros.p                                            }
  41.  
  42. {$IFC UNDEFINED __MEMORY__}
  43. {$I Memory.p}
  44. {$ENDC}
  45. {    MixedMode.p                                                    }
  46.  
  47. {$IFC UNDEFINED __FILES__}
  48. {$I Files.p}
  49. {$ENDC}
  50. {    OSUtils.p                                                    }
  51.  
  52. {$PUSH}
  53. {$ALIGN MAC68K}
  54. {$LibExport+}
  55.  
  56. CONST
  57.     kSIGNewContext                = 1900;
  58.     kSIGDisposeContext            = 1901;
  59.     kSIGSignPrepare                = 1902;
  60.     kSIGSign                    = 1903;
  61.     kSIGVerifyPrepare            = 1904;
  62.     kSIGVerify                    = 1905;
  63.     kSIGDigestPrepare            = 1906;
  64.     kSIGDigest                    = 1907;
  65.     kSIGProcessData                = 1908;
  66.     kSIGShowSigner                = 1909;
  67.     kSIGGetSignerInfo            = 1910;
  68.     kSIGGetCertInfo                = 1911;
  69.     kSIGGetCertNameAttributes    = 1912;
  70.     kSIGGetCertIssuerNameAttributes = 1913;
  71.     kSIGFileIsSigned            = 2500;
  72.     kSIGSignFile                = 2501;
  73.     kSIGVerifyFile                = 2502;
  74.  
  75.     kSIGCountryCode                = 0;
  76.     kSIGOrganization            = 1;
  77.     kSIGStreetAddress            = 2;
  78.     kSIGState                    = 3;
  79.     kSIGLocality                = 4;
  80.     kSIGCommonName                = 5;
  81.     kSIGTitle                    = 6;
  82.     kSIGOrganizationUnit        = 7;
  83.     kSIGPostalCode                = 8;
  84.  
  85.     
  86. TYPE
  87.     SIGNameAttributeType = INTEGER;
  88.  
  89. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  90. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  91. the certificate is currently valid. kSIGPending means the certificate is
  92. currently not valid - but will be.  kSIGExpired means the certificate has
  93. expired. A time is always associated with a SIGCertStatus.  In each case the
  94. time has a specific interpretation.  When the status is kSIGValid the time is
  95. when the certificate will expire. When the status is kSIGPending the time is
  96. when the certificate will become valid. When the status is kSIGExpired the time
  97. is when the certificate expired. In the SIGCertInfo structure, the startDate
  98. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  99. structure, this information is provided in the certSetStatusTime field. In the
  100. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  101. field which can contain any of the types below. NOTE: The only time you will get 
  102. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  103. you get a signature that was created after the certificates expiration date, something
  104. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  105. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  106. possibly allowed by other platforms.
  107. }
  108. { Values for SIGCertStatus or SIGSignatureStatus }
  109.  
  110. CONST
  111.     kSIGValid                    = 0;                            { possible for either a SIGCertStatus or SIGSignatureStatus }
  112.     kSIGPending                    = 1;                            { possible for either a SIGCertStatus or SIGSignatureStatus }
  113.     kSIGExpired                    = 2;                            { possible for either a SIGCertStatus or SIGSignatureStatus
  114.     * possible only for a SIGSignatureStatus }
  115.     kSIGInvalid                    = 3;
  116.  
  117.     
  118. TYPE
  119.     SIGCertStatus = INTEGER;
  120.  
  121.     SIGSignatureStatus = INTEGER;
  122.  
  123. { Gestalt selector code - returns toolbox version in low-order word }
  124.  
  125. CONST
  126.     gestaltDigitalSignatureVersion = 'dsig';
  127.  
  128. { Number of bytes needed for a digest record when using SIGDigest }
  129.     kSIGDigestSize                = 16;
  130.  
  131.     
  132. TYPE
  133.     SIGDigestData = ARRAY [0..kSIGDigestSize-1] OF Byte;
  134.     SIGDigestDataPtr = ^Byte;
  135.  
  136.     SIGCertInfo = RECORD
  137.         startDate:                LONGINT;                                { cert start validity date }
  138.         endDate:                LONGINT;                                { cert end validity date }
  139.         certStatus:                SIGCertStatus;                            { see comment on SIGCertStatus for definition }
  140.         certAttributeCount:        LONGINT;                                { number of name attributes in this cert }
  141.         issuerAttributeCount:    LONGINT;                                { number of name attributes in this certs issuer }
  142.         serialNumber:            Str255;                                    { cert serial number }
  143.     END;
  144.  
  145.     SIGCertInfoPtr = ^SIGCertInfo;
  146.  
  147.     SIGSignerInfo = RECORD
  148.         signingTime:            LONGINT;                                { time of signing }
  149.         certCount:                LONGINT;                                { number of certificates in the cert set }
  150.         certSetStatusTime:        LONGINT;                                { Worst cert status time. See comment on SIGCertStatus for definition }
  151.         signatureStatus:        SIGSignatureStatus;                        { The status of the signature. See comment on SIGCertStatus for definition}
  152.     END;
  153.  
  154.     SIGSignerInfoPtr = ^SIGSignerInfo;
  155.  
  156.     SIGNameAttributesInfo = RECORD
  157.         onNewLevel:                BOOLEAN;
  158.         filler1:                BOOLEAN;
  159.         attributeType:            SIGNameAttributeType;
  160.         attributeScript:        ScriptCode;
  161.         attribute:                Str255;
  162.     END;
  163.  
  164.     SIGNameAttributesInfoPtr = ^SIGNameAttributesInfo;
  165.  
  166.     SIGContextPtr = Ptr;
  167.  
  168.     SIGSignaturePtr = Ptr;
  169.  
  170. {
  171. Certificates are always in order. That is, the signers cert is always 0, the
  172. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  173. You can use this constant for readability in your code.
  174. }
  175.  
  176. CONST
  177.     kSIGSignerCertIndex            = 0;
  178.  
  179. {
  180. Call back procedure supplied by developer, return false to cancel the current
  181. process.
  182. }
  183. TYPE
  184.     SIGStatusProcPtr = ProcPtr;  { FUNCTION SIGStatus: BOOLEAN; }
  185.     SIGStatusUPP = UniversalProcPtr;
  186.  
  187. CONST
  188.     uppSIGStatusProcInfo = $00000010; { FUNCTION : 1 byte result; }
  189.  
  190. FUNCTION NewSIGStatusProc(userRoutine: SIGStatusProcPtr): SIGStatusUPP;
  191.     {$IFC NOT GENERATINGCFM }
  192.     INLINE $2E9F;
  193.     {$ENDC}
  194.  
  195. FUNCTION CallSIGStatusProc(userRoutine: SIGStatusUPP): BOOLEAN;
  196.     {$IFC NOT GENERATINGCFM}
  197.     INLINE $205F, $4E90;
  198.     {$ENDC}
  199. {
  200. Resource id's of standard signature icon suite, all sizes and colors are available.
  201. }
  202.  
  203. CONST
  204.     kSIGSignatureIconResID        = -16800;
  205.     kSIGValidSignatureIconResID    = -16799;
  206.     kSIGInvalidSignatureIconResID = -16798;
  207.  
  208. { ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— 
  209. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  210. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  211. a context for you.  To free the memory occupied by the context and invalidate
  212. its internal data, call SIGDisposeContext. An initialized context has no notion
  213. of the type of operation it will be performing however, once you call
  214. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  215. type is set and to switch  to another type of operation will require creating a
  216. new context. Be sure to pass the same context to corresponding toolbox calls
  217. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  218. signing and verify calls with the same context is not allowed.
  219. }
  220.  
  221. FUNCTION SIGNewContext(VAR context: SIGContextPtr): OSErr;
  222.     {$IFC NOT GENERATINGCFM}
  223.     INLINE $203C, 2, 1900, $AA5D;
  224.     {$ENDC}
  225. FUNCTION SIGDisposeContext(context: SIGContextPtr): OSErr;
  226.     {$IFC NOT GENERATINGCFM}
  227.     INLINE $203C, 2, 1901, $AA5D;
  228.     {$ENDC}
  229. { ——————————————————————————————— SIGNING CALLS ——————————————————————————————— 
  230. Once you have created a SIGContextPtr, you create a signature by calling
  231. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  232. toRcpt SIGSign. To create another signature on different data but for the same
  233. signer, don't dispose of the context and call SIGProcessData for the new data
  234. followed by a call SIGSign again. In this case the signer will not be prompted
  235. for their signer and password again as it was already provided.  Once you call
  236. SIGDisposeContext, all signer information will be cleared out of the context and
  237. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  238. if you want the toolbox to use the last signer by default or prompt for a signer
  239. if none exists.  The prompt parameter can be used to pass a string to be displayed
  240. in the dialog that prompts the user for their password.  If the substring "^1"
  241. (without the quotes) is in the prompt string, then the toolbox will replace it
  242. with the name of the signer from the signer selected by the user.  If an empty
  243. string is passed, the following default string will be sent to the toolbox
  244. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  245. or SIGSign to get information about the signer or certs.
  246. }
  247. FUNCTION SIGSignPrepare(context: SIGContextPtr; {CONST}VAR signerFile: FSSpec; prompt: ConstStr255Param; VAR signatureSize: Size): OSErr;
  248.     {$IFC NOT GENERATINGCFM}
  249.     INLINE $203C, 8, 1902, $AA5D;
  250.     {$ENDC}
  251. FUNCTION SIGSign(context: SIGContextPtr; signature: SIGSignaturePtr; statusProc: SIGStatusUPP): OSErr;
  252.     {$IFC NOT GENERATINGCFM}
  253.     INLINE $203C, 6, 1903, $AA5D;
  254.     {$ENDC}
  255. { ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— 
  256. Once you have created a SIGContextPtr, you verify a signature by calling
  257. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  258. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  259. verified or not (noErr is returned on  success otherwise the appropriate error
  260. code).  Upon successfull verification, you can call any of the utility routines
  261. toRcpt find out who signed the data.
  262. }
  263. FUNCTION SIGVerifyPrepare(context: SIGContextPtr; signature: SIGSignaturePtr; signatureSize: Size; statusProc: SIGStatusUPP): OSErr;
  264.     {$IFC NOT GENERATINGCFM}
  265.     INLINE $203C, 8, 1904, $AA5D;
  266.     {$ENDC}
  267. FUNCTION SIGVerify(context: SIGContextPtr): OSErr;
  268.     {$IFC NOT GENERATINGCFM}
  269.     INLINE $203C, 2, 1905, $AA5D;
  270.     {$ENDC}
  271. { —————————————————————————————— DIGESTING CALLS —————————————————————————————— 
  272. Once you have created a SIGContextPtr, you create a digest by calling
  273. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  274. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  275. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  276. digest.
  277. }
  278. FUNCTION SIGDigestPrepare(context: SIGContextPtr): OSErr;
  279.     {$IFC NOT GENERATINGCFM}
  280.     INLINE $203C, 2, 1906, $AA5D;
  281.     {$ENDC}
  282. FUNCTION SIGDigest(context: SIGContextPtr; VAR digest: SIGDigestData): OSErr;
  283.     {$IFC NOT GENERATINGCFM}
  284.     INLINE $203C, 4, 1907, $AA5D;
  285.     {$ENDC}
  286. { —————————————————————————————— PROCESSING DATA —————————————————————————————— 
  287. To process data during a digest, sign, or verify operation call SIGProcessData
  288. as many times as necessary and with any sized blocks of data.  The data needs to
  289. be processed in the same order during corresponding sign and verify operations
  290. but does not need to be processed in the same sized chunks (i.e., the toolbox
  291. just sees it as a continuous bit stream).
  292. }
  293. FUNCTION SIGProcessData(context: SIGContextPtr; data: UNIV Ptr; dataSize: Size): OSErr;
  294.     {$IFC NOT GENERATINGCFM}
  295.     INLINE $203C, 6, 1908, $AA5D;
  296.     {$ENDC}
  297. { ——————————————————————————————— UTILITY CALLS ——————————————————————————————— 
  298. Given a context that has successfully performed a verification SIGShowSigner
  299. will  display a modal dialog with the entire distinguished name of the person
  300. who signed the data. the prompt (if supplied) will appear at the top of the
  301. dialog.  If no prompt is specified, the default prompt "\pVerification
  302. Successfull." will appear.
  303.  
  304. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  305. successful SIGVerify, you can make the remaining utility calls:
  306.  
  307. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  308. toRcpt index into the certificate set when calling SIGGetCertInfo,
  309. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  310. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  311. will tell you the best status of the entire certificate set while
  312. certSetStatusTime will correspond to the time associated with that status (see
  313. definitions above).
  314.  
  315. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  316. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  317. always the signers certificate.  The  serial number, start date and end date
  318. are there should you wish to display that info.  The  certAttributeCount and
  319. issuerAttributeCount provide the number of parts in the name of that certificate
  320. or that certificates issuer respectively.  You use these numbers to index into
  321. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  322. the name. The certStatus will tell you the status of the certificate while
  323. certStatusTime will correspond to the time associated with that status (see
  324. definitions above).
  325.  
  326. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  327. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  328. tells you wether the name attribute returned is at the same level in the name
  329. hierarchy as the previous attribute.  The type return value tells you  the type
  330. of attribute returned. nameAttribute is the actual string containing the name
  331. attribute.   So, if you wanted to display the entire distinguished name of the
  332. person who's signature was just validated you could do something like this;
  333.  
  334.     (…… variable declarations and verification code would preceed this sample ……)
  335.  
  336.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  337.     HandleErr(error);
  338.  
  339.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  340.         (
  341.         error = SIGGetCertNameAttributes(
  342.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  343.         HandleErr(error);
  344.         DisplayNamePart(theAttribute, type, newLevel);
  345.         )
  346. }
  347. FUNCTION SIGShowSigner(context: SIGContextPtr; prompt: ConstStr255Param): OSErr;
  348.     {$IFC NOT GENERATINGCFM}
  349.     INLINE $203C, 4, 1909, $AA5D;
  350.     {$ENDC}
  351. FUNCTION SIGGetSignerInfo(context: SIGContextPtr; VAR signerInfo: SIGSignerInfo): OSErr;
  352.     {$IFC NOT GENERATINGCFM}
  353.     INLINE $203C, 4, 1910, $AA5D;
  354.     {$ENDC}
  355. FUNCTION SIGGetCertInfo(context: SIGContextPtr; certIndex: LONGINT; VAR certInfo: SIGCertInfo): OSErr;
  356.     {$IFC NOT GENERATINGCFM}
  357.     INLINE $203C, 6, 1911, $AA5D;
  358.     {$ENDC}
  359. FUNCTION SIGGetCertNameAttributes(context: SIGContextPtr; certIndex: LONGINT; attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  360.     {$IFC NOT GENERATINGCFM}
  361.     INLINE $203C, 8, 1912, $AA5D;
  362.     {$ENDC}
  363. FUNCTION SIGGetCertIssuerNameAttributes(context: SIGContextPtr; certIndex: LONGINT; attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  364.     {$IFC NOT GENERATINGCFM}
  365.     INLINE $203C, 8, 1913, $AA5D;
  366.     {$ENDC}
  367. { ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— 
  368. These calls allow you to detect the presence of a standard signtaure in a file as 
  369. well as sign and verify files in a standard way.  An example of this is the Finder, 
  370. which uses these calls to allow the user to "drop sign" a file.
  371.  
  372. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  373. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  374. be returned.
  375.  
  376. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  377. a standard way: 
  378.  
  379. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  380. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  381. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  382. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  383. it is replaced with the newly created signature.
  384.  
  385. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  386. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  387. to SIGShowSigner to display the name of the person who signed the file.
  388. }
  389. FUNCTION SIGFileIsSigned({CONST}VAR fileSpec: FSSpec): OSErr;
  390.     {$IFC NOT GENERATINGCFM}
  391.     INLINE $203C, 2, 2500, $AA5D;
  392.     {$ENDC}
  393. FUNCTION SIGSignFile(context: SIGContextPtr; signatureSize: Size; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusUPP): OSErr;
  394.     {$IFC NOT GENERATINGCFM}
  395.     INLINE $203C, 8, 2501, $AA5D;
  396.     {$ENDC}
  397. FUNCTION SIGVerifyFile(context: SIGContextPtr; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusUPP): OSErr;
  398.     {$IFC NOT GENERATINGCFM}
  399.     INLINE $203C, 6, 2502, $AA5D;
  400.     {$ENDC}
  401.  
  402. {$ALIGN RESET}
  403. {$POP}
  404.  
  405. {$SETC UsingIncludes := DigitalSignatureIncludes}
  406.  
  407. {$ENDC} {__DIGITALSIGNATURE__}
  408.  
  409. {$IFC NOT UsingIncludes}
  410.  END.
  411. {$ENDC}
  412.